﻿/*从数组中删除指定的对象,返回删除是否成功*/
Array.prototype.remove = function (o) {
    var i = this.indexOf(o);
    if (i >= 0) {
        this.splice(i, 1);
        return true;
    } else {
        return false;
    }
};
/*从数组中获取指定属性的最大值,property为空时，默认为DisplayIndex*/
Array.prototype.getMax = function (property) {
    var p = (property == undefined ? 'DisplayIndex' : property);
    var max = undefined;
    this.forEach(function (item) {
        if (max == undefined || item[p] > max) {
            max = item[p];
        }
    });
    return max;
}
var EJToastr = {};
EJToastr.show = function (text, header, type) {
    toastr.options = {
        "closeButton": false,
        "debug": false,
        "positionClass": "toast-bottom-right",
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "3000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEa sing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }
    toastr[type](text, header);
};
EJToastr.info = function (text) { EJToastr.show(text, '提示', "info"); };
EJToastr.warning = function (text) { EJToastr.show(text, '警告', "warning"); };
EJToastr.success = function (text) { EJToastr.show(text, '成功', "success"); };
EJToastr.error = function (text) { EJToastr.show(text, '错误', "error"); };
var EJAlert = {};
EJAlert.show = function (text, header, type) { swal(header, text, type); };
EJAlert.info = function (text) { swal('提示', text, 'info'); };
EJAlert.warning = function (text) { swal('警告', text, 'warning'); };
EJAlert.success = function (text) { swal('成功', text, 'success'); };
EJAlert.error = function (text) { swal('错误', text, 'error'); };
var EJConfim = {};
EJConfim.show = function (text, header, type, showLoader, fn) {
    swal({
        title: header,
        text: text,
        type: type,
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnConfirm: false,
        showLoaderOnConfirm: showLoader
    }, fn);
};
EJConfim.close = function () { swal.close(); };
EJConfim.info = function (text, showLoader, fn) { EJConfim.show(text, '提示', 'info', showLoader, fn); };
EJConfim.warning = function (text, fn) { EJConfim.show(text, '警告', 'warning', false, fn); };
EJConfim.warningWithLoader = function (text, fn) { EJConfim.show(text, '警告', 'warning', true, fn); }

/*获取当前的日期 以 yyyy-MM-dd 格式显示*/
function EJGetDateNow() {
    var d = new Date();
    return EJShowDate(d);
}
function EJGetDateAddDay(days) {
    var d = new Date();
    d.setDate(d.getDate() + days);
    return EJShowDate(d);
}
function EJGetDateAddMonth(months) {
    var d = new Date();
    d.setMonth(d.getMonth() + months);
    return EJShowDate(d);
}
function EJShowDate(d) {
    return d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
}

function EJConvertDate(d) {
    if (d && d.indexOf('/Date(') == 0) {
        var NewDtime = new Date(parseInt(d.replace("/Date(", "").replace(")/", ""), 10));
        var Dyear = NewDtime.getFullYear();
        var Dmonth = NewDtime.getMonth() + 1;
        var Ddate = NewDtime.getDate();
        var dateTime = Dyear + "-" + Dmonth + "-" + Ddate;
        return dateTime;
    } else { return d; }
}
function EJConvertChsDate(d) {
    if (d && d.indexOf('/Date(') == 0) {
        var NewDtime = new Date(parseInt(d.replace("/Date(", "").replace(")/", ""), 10));
        var Dyear = NewDtime.getFullYear();
        var Dmonth = NewDtime.getMonth() + 1;
        var Ddate = NewDtime.getDate();
        var dateTime = Dyear + "年" + Dmonth + "月" + Ddate + "日";
        return dateTime;
    } else { return d; }
}
function EjConvertDateFormat(date, fmt) {
    if (date == undefined) return "";
    if (date) {
        var NewDtime = new Date(parseInt(date.replace("/Date(", "").replace(")/", ""), 10));
        if (NewDtime) {
            return NewDtime.Format(fmt);
        } else {
            return date.Format(fmt);
        }
    } else { return date.Format(fmt); }
}

Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

/*求出当前所处的页索引号段(当前页,总页,总显示索引数)*/
function EJGetPageIndexes(currentIndex, totalPages, showIndexesCount) {
    var begin = 1;
    var end = totalPages;
    if (totalPages > showIndexesCount) {
        var part = parseInt(showIndexesCount / 2);  //求出余数整数部分 4/2=2;5/2=2
        if (currentIndex <= part) {
            begin = 1;
            end = showIndexesCount;
        } else if (currentIndex >= (totalPages - part)) {
            end = totalPages;
            begin = (totalPages - showIndexesCount) + 1;
        } else {
            begin = currentIndex - part;
            end = (begin + showIndexesCount) - 1;
        }
    }
    var arr = [];
    for (var i = begin; i <= end; i++) {
        arr.push(i);
    }
    return arr;
}